
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;

import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CreateShortCut {
    private static String suffix = ".md";
    private static String saveDri = "F:\\my project\\test\\快速导航.md";
    private static String[] excludeName = new String[]{"readme", "help", "history", "CHANGE", "CONTRIBUTING","npm","LICENSE","package"};

    static {
        try {
            new File(saveDri).createNewFile();
            //excludeName小写转换
            for (int i = 0; i < excludeName.length; i++) {
                excludeName[i]=excludeName[i].toLowerCase();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        String startDir = "F:\\";
        run(startDir);
    }

    /*    public static void run(String startDir) throws IOException {
            File file = new File(startDir);
            List<File> files = traverseFolder(file);
            for (File f : files) {
                if (f.getName().endsWith(suffix)){
                    String[] split = f.getAbsolutePath().split("\\\\");
                    MdUrl head = new MdUrl();
                    MdUrl next = head;
                    int count=0;
                    for (int i = 0; i < split.length; i++) {
                        if (i!=0&&i!=split.length-1){
                            String substring = f.getAbsolutePath().substring(0, count + split[i].length());
                            MdUrl mdUrl = new MdUrl(split[i], new File(substring));
                            next.next=mdUrl;
                            next=next.next;
                        }
                        count+=split[i].length()+1;
                    }
                    MdUrl mdUrl = new MdUrl(f.getName(), f);
                    next.next=mdUrl;
                    System.out.println(head);
                }
            }

        }
        */

    public static void run(String startDir) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(saveDri)));
        File file = new File(startDir);
        List<File> files = traverseFolder(file, suffix);
        String result;
        for (File f : files) {
            //去掉隐藏文件夹 并 排除文件
            if (!getPositiveFileLevelName(f, 1).startsWith("$") && !isExclude(f)) {
                result = "<a href=\"" + f.getAbsolutePath() + "\">" + getInvertedFileLevelName(f, 2) + "：" + f.getName() + "</a><br>\n";
                result = "[" + getInvertedFileLevelName(f, 2) + "：" + f.getName() + "](" + f.getAbsolutePath() + ")\n\n";
                System.out.println(result);
                bufferedWriter.write(result);
            }
        }
        bufferedWriter.close();
    }

    public static boolean isExclude(File file) {
        for (String s : excludeName) {
            if (file.getName().toLowerCase().startsWith(s) || file.getName().toLowerCase().startsWith(s)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 正数的文件层级
     *
     * @param file
     * @param level
     * @return
     */
    public static String getPositiveFileLevelName(File file, Integer level) {
        String[] split = file.getAbsolutePath().split("\\\\");
        if (level == null || level < 0 || level > split.length) {
            throw new RuntimeException("文件路径层级：level错误，找不到" + level);
        }
        return split[level];
    }

    /**
     * 倒数的文件层级
     *
     * @param file
     * @param level
     * @return
     */
    public static String getInvertedFileLevelName(File file, Integer level) {
        String[] split = file.getAbsolutePath().split("\\\\");
        if (level == null || level < 0 || level > split.length) {
            throw new RuntimeException("文件路径层级：level错误，找不到" + level);
        }
        return split[split.length - level];
    }

    private static List<File> fileList = new ArrayList<>();

    /**
     * 递归查询所有指定文件
     *
     * @param file
     * @return
     */
    public static List<File> traverseFolder(File file, String suffix) {
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null == files || files.length == 0) {
//                log.debug("文件夹是空的!");
                return null;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
//                        log.debug("文件夹:" + file2.getAbsolutePath());
                        traverseFolder(file2, suffix);
                    } else {
                        if (file2.getName().endsWith(suffix)) {
                            fileList.add(file2);
//                            log.debug("文件:" + file2.getAbsolutePath());
                        }
                    }
                }
            }
        } else {
//            log.debug("文件不存在!");
        }
        return fileList;
    }

    /**
     * 递归查询所有文件
     *
     * @param file
     * @return
     */
    public static List<File> traverseFolder(File file) {
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null == files || files.length == 0) {
//                log.debug("文件夹是空的!");
                return null;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
//                        log.debug("文件夹:" + file2.getAbsolutePath());
                        traverseFolder(file2);
                    } else {
                        fileList.add(file2);
//                        log.debug("文件:" + file2.getAbsolutePath());
                    }
                }
            }
        } else {
//            log.debug("文件不存在!");
        }
        return fileList;
    }


}